//
//  CategoryController.m
//  JiaoX
//
//  Created by 尹桥印 on 14/11/14.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXCategoryController.h"
#import "CategoryDataTool.h"
#import "ProductCategory.h"
#import "CategoryInfo.h"
#import "CategoryCell.h"
#import "CategoryCellView.h"
#import "CategoryGroupView.h"
#import "CategoryCellChildView.h"
#import "JXProdListController.h"


@interface JXCategoryController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,CategoryGroupViewDelegate,CategoryCellViewDelegate>{
    UITableView *_tableView;
}
@property (nonatomic, strong) NSMutableArray *categorys;
@end

@implementation JXCategoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加搜索
    UISearchBar *searchBar = [[UISearchBar alloc]init];
    searchBar.delegate = self;
    searchBar.placeholder = @"想爱爱 搜一下";
    [searchBar setTintColor:[UIColor whiteColor]];
    [searchBar setBarTintColor:[UIColor clearColor]];
    
    self.navigationItem.titleView = searchBar;
    
    _categorys = [NSMutableArray array];
    UITableView *tableVeiw = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    tableVeiw.sectionFooterHeight = 10;
    tableVeiw.sectionHeaderHeight = 0;
    //添加头部分割条
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, JXScreenW, 10)];
    view.backgroundColor = [UIColor clearColor];
    [tableVeiw setTableHeaderView:view];
    
    //设置tableview
    [self.view addSubview:tableVeiw];
    _tableView = tableVeiw;
    _tableView.delegate  = self;
    _tableView.dataSource = self;
    [self requestData];
}

-(void)requestData{
    [CategoryDataTool categoryDataInfoWithParam:[[[JXBaseRequest alloc]init] keyValues] success:^(CategoryInfo *result) {
        //加载成功
        [_categorys removeAllObjects];
        [_categorys addObjectsFromArray:result.productCategory];
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - Table view 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _categorys.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ProductCategory *prod = _categorys[section];
    if([prod isClose]){//如果是关闭状态 ，返回0；
        return 0;
    }
    
    return 1;//每一组返回多少条数据
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CategoryCell *cell = [CategoryCell cellWithTableView:tableView];
    cell.cellView.delegate = self;
    ProductCategory *prod = _categorys[indexPath.section];

    [cell setSubCagetoryList:prod.subCategoryList];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



#pragma mark - Table view 代理方法

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ProductCategory *prod = _categorys[section];
    CategoryGroupView *view = [CategoryGroupView initWithIcon:prod.categoryIcon title:prod.categoryName status:prod.isClose];
    view.section = section;
    view.btnClickDelegate = self;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CateGroupViewHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductCategory *prod = _categorys[indexPath.section];
    //计算出行数
    int rowNum = (int)(prod.subCategoryList.count + SUBCATEGORY_COL-1)/SUBCATEGORY_COL;
    return (CATEGORY_CHILDVIEW_HEIGHT+JXPublicInset*2)*rowNum+TITLE_FONT_SIZE+rowNum*15;
}



#pragma mark - Table view searchbar代理方法
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
    [searchBar setShowsCancelButton:YES animated:YES];
    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
        
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            UIButton * cancel =(UIButton *)view;
            [cancel setTitle:@"取消" forState:UIControlStateNormal];
        }
    }
    return true;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    [searchBar setShowsCancelButton:NO animated:YES];
}

#pragma mark - 组标题的点击事件代理方法
- (void)categoryGroupViewClick:(NSInteger)section{
    //将其状态置反
    ProductCategory *prod = _categorys[section];
    BOOL tempBool = [prod isClose];
    prod.isClose =!tempBool;
    [_tableView reloadData];
}
#pragma mark - CategoryCellViewDelegate
- (void)subItemClickDelegate:(SubCategory *)sub{
    
    JXProdListController *prodList = [[JXProdListController alloc]initWithSubCategory:sub];
    [self.navigationController pushViewController:prodList animated:YES];
}

////初图将移出屏幕
//-(void)viewWillDisappear:(BOOL)animated{
//    if(_categorys.count==0){
//        //重新请求数据
//        [self requestData];
//    }
//}

@end
